<?php 
class ControllerProductSpecial extends FrontController { 	
	public function index() { 
		
		M('catalog/product');		
		
		$sort = $this->request->get('sort', 'sort_order');
		$order = $this->request->get('order', 'asc');
		$page = $this->request->get('page', 1);
		$limit = $this->request->get('limit', C('config_catalog_limit'));
		
		$this->document->setTitle($this->_['heading_title']);

		$url = $this->request->query('sort,order,page,limit');
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->_['heading_title'],
			'href'      => U('product/special', $url),
      		'separator' => $this->_['text_separator']
   		);
		
		$this->data['text_compare'] = sprintf($this->_['text_compare'], (isset($this->session->data['compare']) ? count($this->session->data['compare']) : 0));
		
		$this->data['products'] = array();

		$data = array(
			'sort'  => $sort,
			'order' => $order,
			'start' => ($page - 1) * $limit,
			'limit' => $limit
		);
			
		$product_total = $this->model_catalog_product->getTotalProductSpecials($data);
			
		$results = $this->model_catalog_product->getProductSpecials($data);
			
		foreach ($results as $result) {
						
			$this->data['products'][] = array(
				'product_id'  => $result['product_id'],
				'thumb'       => thumb($result['image'], C('config_image_product_width'), C('config_image_product_height')),
				'name'        => $result['name'],
				'summary'     => v5_html_entity_decode($result['summary']),
				'price'       => pprintf($result['price'], $result['tax_class_id']),
				'special'     => pprintf($result['special'], $result['tax_class_id']),
				'tax'         => C('config_tax') ? currency((float)$result['special'] ? $result['special'] : $result['price']) : false,
				'rating'      => C('config_review_status') ? $result['rating'] : false,
				'reviews'     => sprintf($this->_['text_reviews'], (int)$result['reviews']),
				'href'        => $result['link'],
			);
		}

		$url = '';

		if (isset($this->request->get['limit'])) {
			$url .= '&limit=' . $this->request->get['limit'];
		}
			
		$this->data['sorts'] = array();
		
		$this->data['sorts'][] = array(
			'text'  => $this->_['text_default'],
			'value' => 'sort_order-asc',
			'href'  => U('product/special', 'sort=sort_order&order=asc' . $url)
		);
		
		$this->data['sorts'][] = array(
			'text'  => $this->_['text_price_asc'],
			'value' => 'price-asc',
			'href'  => U('product/special', 'sort=price&order=asc' . $url)
		); 

		$this->data['sorts'][] = array(
			'text'  => $this->_['text_price_desc'],
			'value' => 'price-desc',
			'href'  => U('product/special', 'sort=price&order=desc' . $url)
		); 
		
		$this->data['sorts'][] = array(
				'text'  => $this->_['text_release_date'],
				'value' => 'date_added-desc',
				'href'  => U('product/special', 'sort=date_added&order=desc' . $url)
		);
		
		if (C('config_review_status')) {	
			$this->data['sorts'][] = array(
				'text'  => $this->_['text_rating_desc'],
				'value' => 'rating-desc',
				'href'  => U('product/special', 'sort=rating&order=desc' . $url)
			);
			
			$this->data['sorts'][] = array(
				'text'  => $this->_['text_most_reviews'],
				'value' => 'reviews-desc',
				'href'  => U('product/special', 'sort=reviews&order=desc' . $url)
			);
		}

		
		$url = $this->request->query('sort,order');

		$this->data['limits'] = array();
		
		$this->data['limits'][] = array(
			'text'  => C('config_catalog_limit'),
			'value' => C('config_catalog_limit'),
			'href'  => U('product/special', $url . '&limit=' . C('config_catalog_limit'))
		);
		
		for($i = 25; $i <= 100; $i += 25) {		
			$this->data['limits'][] = array(
				'text'  => $i,
				'value' => $i,
				'href'  => U('product/special', $url . "&limit=$i")
			);
		}

		$url = $this->request->query('sort,order,limit');
		$url = U('product/special', $url . '&page={page}');			
		$this->data['pagination'] = $this->pagination($product_total, $page, $url, $limit)->render();
			
		$this->data['sort'] = $sort;
		$this->data['order'] = $order;
		$this->data['limit'] = $limit;
		
		$this->display('product/special.tpl');
  	}
}
?>